/**
 * Copyright 2014 opencxa.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.cxa.rpc.cloud.server.components.authentication;


import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;


/**
 * @author Christopher Armenio
 *
 */
public abstract class DatabaseManager
{
	public static final Group GROUP_ADMIN = new Group(0, "admin");
	public static User USER_ROOT = null;
	
	
	/**
	 * An exception specifically designed for exceptions occurring
	 * when interacting with a database
	 */
	public static class DatabaseManagerException extends Exception
	{
		private static final long serialVersionUID = -2299623560591652666L;

		/**
		 * Creates a DatabaseManagerException with the given exception as the cause
		 * 
		 * @param eIn the cause of the exception
		 */
		public DatabaseManagerException(Exception eIn)
		{
			super(eIn);
		}
		
		/**
		 * Creates a DatabaseManagerException with the provided message
		 * 
		 * @param messageIn the message describing the exception
		 */
		public DatabaseManagerException(String messageIn)
		{
			super(messageIn);
		}
	}
	
	
	protected final Logger logger = LogManager.getLogger(this.toString());
	
	
	public void enableRootUser(String rootUserPasswordIn)
	{
		USER_ROOT = new User("root@localhost", rootUserPasswordIn, new Integer[]{GROUP_ADMIN.getId()}, true);
	}
	
	
	public List<User> getUserList() throws DatabaseManagerException
	{
		List<User> retVal = new ArrayList<User>();
		if( USER_ROOT != null ) retVal.add(USER_ROOT);
		return retVal;
	}
	
	
	public List<Group> getGroupList() throws DatabaseManagerException
	{
		List<Group> retVal = new ArrayList<Group>();
		retVal.add(GROUP_ADMIN);
		return retVal;
	}
	
	
	public User getUser_byEmail(String emailIn) throws DatabaseManagerException
	{
		if( (USER_ROOT != null) && USER_ROOT.getEmail().equals(emailIn) ) return USER_ROOT;
		return null;
	}
	
	
	public Group getGroup_byId(Integer idIn) throws DatabaseManagerException
	{
		return (GROUP_ADMIN.getId() == idIn) ? GROUP_ADMIN : null;
	}
	
	
	public abstract void addUser(User userIn) throws DatabaseManagerException;
	public abstract boolean updateUser(User userIn) throws DatabaseManagerException;
	public abstract boolean deleteUserByEmail(String emailIn) throws DatabaseManagerException;
	public abstract void addGroup(Group groupIn) throws DatabaseManagerException;
	public abstract boolean deleteGroupById(Integer idIn) throws DatabaseManagerException;
	public abstract boolean updateGroup(Group groupIn) throws DatabaseManagerException;
	
	
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		return String.format("%s.%s", this.getClass().getSimpleName(), System.identityHashCode(this));
	}
}
